package org.wak.action.actions
{
	import org.wak.action.IInteractable;
	import org.wak.main.AdventureMaster;
	import org.wak.main.PuzzleControllerBase;
	import org.wak.utils.IIterator;
	
	public class UseAction extends BaseAction
	{
		//private var mParam1:IInteractable = null ;
		private var mParam2:IInteractable = null ;
		
		private var mUseConnector:String = "" ;
		
		public function UseAction()
		{
			super();
			mActionId = AdventureActions.ActionIdUse ;
			mActionText = AdventureMaster.Texts.UseText ;
			mUseConnector = AdventureMaster.Texts.UseConnector ;
		}
		
		public override function AddParameter(pItem:IInteractable):void
		{
			if( mParam1 != null )
			{
				mParam2 = pItem ;
			}
			
			if( mParam1 == null )
			{
				mParam1 = pItem ;
			}
		}
		
		public override function TestParameter(pItem:IInteractable):Boolean
		{
			if( mParam1 == null )
			{
				return true ;
			}
			else
			{
				if( pItem != mParam1 )
				{
					return true ;
				}
			}
			
			return false ;
		}
		
		public override function get isReady():Boolean
		{
			if(mParam1 != null)
			{
				if(mParam1.unitaryUse)
					return true;
			}
			
			if((mParam1 != null) && (mParam2 != null))
			{
				return true;
			}
			
			return false;
		}
		
		public override function GetText():String
		{
			var fRetText:String = mActionText ;
			
			if( mParam1 != null)
				fRetText += " " + mParam1.interactionName + " " + mUseConnector ; 
				
			if( mParam2 != null)
				fRetText += " " +mParam2.interactionName ;
			
			//trace("Use GetText: "+fRetText)	;
			return fRetText ;
		}
		
		public override function Execute(pIterator:IIterator):Boolean 
		{
			var fMethodFound:Boolean = false ;
			
			var fPuzzleController:PuzzleControllerBase = null;
			
			var fMethodEventName:String = "" ;
			
			// create the method name .
			if( mParam1.unitaryUse )
			{
				fMethodEventName = mActionId+mParam1.interactableId ;
			}
			else
			{
				fMethodEventName = mActionId+mParam1.interactableId+"And"+mParam2.interactableId ;
			}
			
			
			while( pIterator.hasNext() )
			{
				fPuzzleController = pIterator.next() as PuzzleControllerBase ;
				fMethodFound =  fPuzzleController.hasOwnProperty(fMethodEventName) ;
				if( fMethodFound )
				{
					if(!(mParam1.unitaryUse))
					{
						fPuzzleController[fMethodEventName](mParam1,mParam2);
					}
					else
					{
						fPuzzleController[fMethodEventName](mParam1);
					}
				}
			}
			
			return fMethodFound
		}
		
		public override function CanExecute(pIterator:IIterator):Boolean 
		{
			var fMethodFound:Boolean = false ;
			
			var fPuzzleController:PuzzleControllerBase = null;
			
			var fMethodEventName:String = "" ;
			
			// create the method name .
			if( mParam1.unitaryUse )
			{
				fMethodEventName = mActionId+mParam1.interactableId ;
			}
			else
			{
				fMethodEventName = mActionId+mParam1.interactableId+"And"+mParam2.interactableId ;
			}
			
			
			while( pIterator.hasNext() )
			{
				fPuzzleController = pIterator.next() as PuzzleControllerBase ;
				fMethodFound = fMethodFound || fPuzzleController.hasOwnProperty(fMethodEventName) ;
				if( fMethodFound )
				{
					return true
				}
			}
			
			return false
		}
		
		public override function GetNoActionText():String
		{
			return AdventureMaster.Texts.UseNoAction ;
		} 
		
		/** @inheritDoc		 */
		public override function GetParam2():IInteractable 
		{
			return mParam2 ;
		}
		
		/** @inheritDoc		 */
		public override function get paramsNumber():int
		{
			return 2 ;
		}
		
	}
}